package com.view.jameson.androidrecyclerviewcard.hv;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.view.jameson.androidrecyclerviewcard.CardViewBean;
import com.view.jameson.androidrecyclerviewcard.R;
import com.view.jameson.androidrecyclerviewcard.hv2.DemoAdapter;
import com.view.jameson.androidrecyclerviewcard.hv2.GalleryLayoutManager;
import com.view.jameson.androidrecyclerviewcard.hv2.GalleryLayoutManager2;
import com.view.jameson.androidrecyclerviewcard.hv2.HorizontalSpaceItemDecoration;
import com.view.jameson.androidrecyclerviewcard.hv2.ScaleTransformer;
import com.view.jameson.library.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity3 extends Activity {
    private Activity mActivity;
    private HorizontalLoopView hlv1;
    private TextView tv1;
    private String[] mImages;
    private ImageView iv2;
    RecyclerView mMainRecycle1;
    private int selectPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mActivity = this;
//        initHlv1();

        initH1v2();


        mMainRecycle1 = findViewById(R.id.main_recycle1);
        mMainRecycle1.addItemDecoration(new HorizontalSpaceItemDecoration(4));
        initView2();



    }


    private void initView2(){


        final List<CardViewBean> title = new ArrayList<CardViewBean>();
        int size = 3;
        title.add(new CardViewBean("https://m.bbmaiqiu.com/api/forehead/system/file/get/65812fed0419d3282052e1ba.jpeg"));
        title.add(new CardViewBean("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png"));
        GalleryLayoutManager2 layoutManager1 = new GalleryLayoutManager2(GalleryLayoutManager.HORIZONTAL);
        layoutManager1.attach(mMainRecycle1, title.size() * 10000);


        layoutManager1.setItemTransformer(new ScaleTransformer());


        DemoAdapter demoAdapter1 = new DemoAdapter(this,title) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//                mMainTvRecycleInfo1.append("Create VH type:+" + viewType + "\n");
                return super.onCreateViewHolder(parent, viewType);
            }
        };

        mMainRecycle1.setAdapter(demoAdapter1);


        layoutManager1.setOnItemSelectedListener(new GalleryLayoutManager2.OnItemSelectedListener() {
            @Override
            public void onItemSelected(RecyclerView recyclerView, View item, int position) {
                Log.e("当前选中的", position + "==");
                selectPosition = position;
//                for (int i = 0; i < title.size(); i++) {
//                    title.get(i).setSelected(false);
//                }
//                title.get(position % title.size()).setSelected(true);
////                demoAdapter1.notifyDataSetChanged();
//                recyclerView.post(new Runnable() {
//                    @Override
//                    public void run() {
////                        demoAdapter1.notifyDataSetChanged();
//                        demoAdapter1.notifyItemChanged(position % title.size());
//                        demoAdapter1.notifyItemChanged(position);
//                    }
//                });
                View viewLeft1 = recyclerView.getLayoutManager().findViewByPosition(position-1);
                View viewLeft2 = recyclerView.getLayoutManager().findViewByPosition(position-2);
                View viewRight1 = recyclerView.getLayoutManager().findViewByPosition(position+1);
                View viewRight2 = recyclerView.getLayoutManager().findViewByPosition(position+2);
                GradientDrawable borderDrawable1 = new GradientDrawable();
                borderDrawable1.setShape(GradientDrawable.RECTANGLE);
                borderDrawable1.setColor(Color.TRANSPARENT); // 设置填充颜色
                borderDrawable1.setStroke(1, Color.parseColor("#A1A0A5")); // 设置边框宽度和颜色
                borderDrawable1.setCornerRadius(8f); // 设置圆角半径

                GradientDrawable borderDrawable2 = new GradientDrawable();
                borderDrawable2.setShape(GradientDrawable.RECTANGLE);
                borderDrawable2.setColor(Color.TRANSPARENT); // 设置填充颜色
                borderDrawable2.setStroke(2, Color.parseColor("#f80069")); // 设置边框宽度和颜色
                borderDrawable2.setCornerRadius(8f); // 设置圆角半径
                if (viewLeft1!=null){
//                    viewLeft1.setBackground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
//                    setMeg(viewLeft1,margin);
//                    viewLeft1.setScaleX(1);
//                    viewLeft1.setScaleY(1);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        viewLeft1.setForeground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
                    }
                }
                if (viewLeft2!=null){
//                    viewLeft2.setBackground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
//                    setMeg(viewLeft2,margin);
//                    viewLeft2.setScaleX(1);
//                    viewLeft2.setScaleY(1);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        viewLeft2.setForeground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
                    }
                }
                if (viewRight1!=null){
//                    viewRight1.setBackground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
//                    setMeg(viewRight1,margin);
//                    viewRight1.setScaleX(1);
//                    viewRight1.setScaleY(1);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        viewRight1.setForeground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
                    }
                }
                if (viewRight2!=null){
//                    viewRight2.setBackground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
//                    setMeg(viewRight2,margin);
//                    viewRight2.setScaleX(1);
//                    viewRight2.setScaleY(1);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        viewRight2.setForeground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_a1a0a5_storke_2_r8));
                    }
                }
                View view = recyclerView.getLayoutManager().findViewByPosition(position);
//                view.setBackground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_f80069_storke_2_r8));
//                view.getLayoutParams().width= ScreenUtil.dip2px(view.getContext(),96);
//                view.getLayoutParams().height=ScreenUtil.dip2px(view.getContext(),54);
//                view.setScaleX(1.2f);
//                view.setScaleY(1.2f);

//                int margin = DensityUtils.dp2px(mActivity, 4);
//                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
//
//                marginLayoutParams.leftMargin = margin;
//                marginLayoutParams.rightMargin = margin;
//                view.setLayoutParams(marginLayoutParams);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    view.setForeground(mActivity.getDrawable(com.view.jameson.library.R.drawable.shape_f80069_storke_2_r8));
                }

            }
        });

        Button btn_001 = findViewById(R.id.btn_001);
        Button btn_002 = findViewById(R.id.btn_002);
        Button btn_003 = findViewById(R.id.btn_003);

        btn_001.setOnClickListener(view -> {
            int po = selectPosition - 1;
            if (selectPosition <= 1) {
                po = 0;
            }
            mMainRecycle1.smoothScrollToPosition(po);
        });
        btn_002.setOnClickListener(view -> {
            mMainRecycle1.smoothScrollToPosition(selectPosition + 1);
        });
        btn_003.setOnClickListener(view -> {
            mMainRecycle1.post(new Runnable() {
                @Override
                public void run() {
//                    mMainRecycle1.scrollToPosition(0);
//                    layoutManager1.setPosition(100001);




                }
            });

//            layoutManager1.scrollToPosition(90 % title.size());
        });
    }

    /**
     * 初始化视图二
     */
    private void initH1v2() {
        HorizontalLoopView hlv2 = (HorizontalLoopView) findViewById(R.id.hlv2);
        iv2 = (ImageView) findViewById(R.id.iv2);


        mImages = getResources().getStringArray(R.array.images_url);
        if (mImages.length == 1) {
//            hlv2.setIsLoop(false);
        }
        hlv2.setLoopViewAdapter(new LoopViewAdapter<View>() {
            @Override
            protected int setCenterIndex() {
                return mImages.length / 2;
            }

            @Override
            public int getChildWidth() {
                return DensityUtils.dp2px(mActivity, 80f);
            }

            @Override
            public int getItemCount() {
                return mImages.length;
            }

            @Override
            public View getView(int position, boolean isCenter) {
//                ImageView view = new ImageView(mActivity);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                View view = getLayoutInflater().inflate(R.layout.layout_image, null);
                view.setSelected(isCenter);
                params.width = getChildWidth();
                view.setLayoutParams(params);

                if (isCenter) {
                    ImageView iv = (ImageView) view.findViewById(R.id.iv);
                    int margin = DensityUtils.dp2px(mActivity, 1);
                    ViewGroup.MarginLayoutParams marginLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

                    marginLayoutParams.leftMargin = margin;
                    marginLayoutParams.rightMargin = margin;
                    marginLayoutParams.topMargin = margin;
                    marginLayoutParams.bottomMargin = margin;
                    iv.setLayoutParams(marginLayoutParams);
                }


                return view;
            }

            @Override
            public void setData(View scrollView, int position) {
                ImageView iv = (ImageView) scrollView.findViewById(R.id.iv);

                Glide.with(mActivity)
                        .load(mImages[position])
                        .into(iv);
            }

            @Override
            public void onSelect(View selectView, int position) {
                Glide.with(mActivity)
                        .load(mImages[position])
                        .into(iv2);
                Log.e("当前选择的position", position + "==");
            }
        });
    }

//    private void initHlv1() {
//        hlv1 = (HorizontalLoopView) findViewById(R.id.hlv1);
//        tv1 = (TextView) findViewById(R.id.tv1);
//
//        final String[] mMonths = getResources().getStringArray(R.array.months_chines);
//
//        hlv1.setLoopViewAdapter(new LoopViewAdapter<TextView>() {
//            @Override
//            protected int setCenterIndex() {
//                return 5;
//            }
//
//            @Override
//            public int getChildWidth() {
//                return DensityUtils.dp2px(mActivity, 80F);
//            }
//
//            @Override
//            public int getItemCount() {
//                return mMonths.length;
//            }
//
//            @Override
//            public TextView getView(int position, boolean isCenter) {
//                //创建TextView
//                TextView textView = new TextView(mActivity);
//                //布局参数
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
//                params.width = getChildWidth();
//                textView.setLayoutParams(params);
//
//                //test 选中中心的那个
//                textView.setSelected(isCenter);
//                textView.setBackgroundResource(R.drawable.select_text_bg);
//                textView.setTextColor(getResources().getColorStateList(R.color.select_text_text_color));
//                textView.setGravity(Gravity.CENTER);
//                textView.setTextSize(DensityUtils.sp2px(mActivity, 12));
//                return textView;
//            }
//
//            @Override
//            public void setData(TextView scrollView, int position) {
//                //设置数据
//                scrollView.setText(mMonths[position]);
//            }
//
//            @Override
//            public void onSelect(TextView selectView, int position) {
//                tv1.setText("已选择:" + position);
//            }
//        });
//    }

}
